Clover coverage report - Flock Flock - 0.7-dev
Coverage timestamp: Thu Jan 30 2003 01:35:37 EST
file stats: LOC: 123   Methods: 2
NCLOC: 95   Classes: 1
This license of Clover is provided to support the development of Flock only. Please visit http://www.thecortex.net/clover to obtain a licensed version of Clover.
 
 Source file Conditionals Statements Methods TOTAL
HTMLUtil.java 77.3% 78.8% 100% 78.9%
 1   
 package net.sf.flock.parser;
 2   
 
 3   
 /**
 4   
  * Utility class for dealing with HTML entities.
 5   
  * 
 6   
  * @version $Revision: 1.1 $
 7   
  * @author $Author: phraktle $
 8   
  */
 9   
 public class HTMLUtil {
 10   
 
 11   
     private final static String[][] ENTITIES =
 12   
          {{  "&lt;"     , "<" } ,
 13   
           {  "&gt;"     , ">" } ,
 14   
           {  "&amp;"    , "&" } ,
 15   
           {  "&quot;"   , "\"" } ,
 16   
           {  "&agrave;" , "à" } ,
 17   
           {  "&Agrave;" , "À" } ,
 18   
           {  "&acirc;"  , "â" } ,
 19   
           {  "&auml;"   , "ä" } ,
 20   
           {  "&Auml;"   , "Ä" } ,
 21   
           {  "&Acirc;"  , "Â" } ,
 22   
           {  "&aring;"  , "å" } ,
 23   
           {  "&Aring;"  , "Å" } , 
 24   
           {  "&aelig;"  , "æ" } , 
 25   
           {  "&AElig;"  , "Æ" } ,
 26   
           {  "&ccedil;" , "ç" } ,
 27   
           {  "&Ccedil;" , "Ç" } ,
 28   
           {  "&eacute;" , "é" } ,
 29   
           {  "&Eacute;" , "É" } ,
 30   
           {  "&egrave;" , "è" } ,
 31   
           {  "&Egrave;" , "È" } ,
 32   
           {  "&ecirc;"  , "ê" } ,
 33   
           {  "&Ecirc;"  , "Ê" } ,
 34   
           {  "&euml;"   , "ë" } ,
 35   
           {  "&Euml;"   , "Ë" } ,
 36   
           {  "&iuml;"   , "ï" } , 
 37   
           {  "&Iuml;"   , "Ï" } ,
 38   
           {  "&ocirc;"  , "ô" } ,
 39   
           {  "&Ocirc;"  , "Ô" } ,
 40   
           {  "&ouml;"   , "ö" } ,
 41   
           {  "&Ouml;"   , "Ö" } ,
 42   
           {  "&oslash;" , "ø" } ,
 43   
           {  "&Oslash;" , "Ø" } ,
 44   
           {  "&szlig;"  , "ß" } ,
 45   
           {  "&ugrave;" , "ù" } ,
 46   
           {  "&Ugrave;" , "Ù" } ,
 47   
           {  "&ucirc;"  , "û" } ,
 48   
           {  "&Ucirc;"  , "Û" } , 
 49   
           {  "&uuml;"   , "ü" } ,
 50   
           {  "&Uuml;"   , "Ü" } ,
 51   
           {  "&nbsp;"   , " " } ,
 52   
           {  "&reg;"    , "\u00a9" } ,
 53   
           {  "&copy;"   , "\u00ae" } ,
 54   
           {  "&euro;"   , "\u20a0" } };
 55   
 
 56   
 
 57  4
     public static String unescape(String source) {
 58  4
         if (source==null || source.indexOf('&')==-1) {
 59   
             // no entities
 60  1
             return source;
 61   
         }
 62  3
         int len = source.length();
 63  3
         StringBuffer result = new StringBuffer(len);
 64  3
         for (int i=0; i<len; i++) {
 65  5
             int startPos = source.indexOf('&', i);
 66   
 
 67  5
             if (startPos!=i) {
 68   
                 // we skipped some chars, append them to result
 69  4
                 result.append(source.substring(i, startPos==-1 ? len : startPos));
 70   
             }
 71   
 
 72  5
             if (startPos==-1) {
 73   
                 // no more entities
 74  1
                 break;    
 75   
             }
 76   
 
 77  4
             int endPos = source.indexOf(';', startPos);
 78  4
             if (endPos==-1) {
 79   
                 // broken entity
 80  1
                 result.append( source.substring(startPos) );
 81  1
                 break;
 82   
             }
 83   
 
 84  3
             String entity = source.substring(startPos, endPos+1);
 85  3
             int p=0;
 86  3
             for (; p<ENTITIES.length; p++) {
 87  46
                 if (entity.equals(ENTITIES[p][0])) {
 88  2
                     result.append( ENTITIES[p][1] );
 89  2
                     break;
 90   
                 }
 91   
             }
 92  3
             if (p>=ENTITIES.length) {
 93   
                 // no entity replacement found, leave as-is
 94  1
                 result.append(entity);
 95   
             }
 96   
 
 97   
             // skip ahead a littlebit
 98  3
             i=endPos;
 99   
         }
 100  3
         return result.toString();
 101   
     }
 102   
 
 103  1
     public static String escape(String source) {
 104  1
         if (source==null)
 105  1
             return null;
 106   
 
 107  0
         StringBuffer result = new StringBuffer(source.length());
 108   
 
 109  0
         for (int i=0;i<source.length();i++) {
 110   
 
 111  0
             char ch = source.charAt(i);
 112   
 
 113  0
             if ((int)ch>127) {
 114  0
                 result.append("&#").append((int)ch).append(';');
 115   
             } else {
 116  0
                 result.append(ch);
 117   
             }
 118   
         }
 119  0
         return result.toString();
 120   
     }
 121   
 
 122   
 }
 123